package com.svo.weitu.model.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.svo.weitu.model.entity.ListEntity;

public class ListData {
	private Context context;
	private DbHelper dbHelper;
	public ListData(Context context) {
		this.context = context;
		dbHelper = DbHelper.getInstance(context);
	}
	
	public long insert(ListEntity listEntity, String from) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		try {
			ContentValues values = new ContentValues();
			values.put("title", listEntity.getTitle());
			values.put("parId", listEntity.getParId());
			values.put("thumbnail", listEntity.getThumbnail());
			values.put("img_urls", listEntity.getUrls());
			values.put("insert_time", listEntity.getTime());
			values.put("description", listEntity.getDesc());
			values.put("loves", listEntity.getLoves());
			values.put("_id", listEntity.getId());
			if ("home".equals(from)) {
				values.put("isHome", "yes");
			}
			return db.insertWithOnConflict("list", null, values,SQLiteDatabase.CONFLICT_IGNORE);
		} catch (Exception e) {
			db.execSQL("update list set isHome = ? where _id = ?",new String[]{"yes",listEntity.getId()+""});
			return -1;
		}
	}
	/**
	 * 查询某个资源的前十条信息
	 * @param i 资源ID
	 * @return
	 */
	public List<ListEntity> queryAll(int i) {
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		String sql = "select * from list where isHome = 'yes' order by insert_time desc limit 10";
		Cursor cursor;
		if (i != 1) {
			sql = "select * from list where parId = ? order by insert_time desc limit 10";
			cursor = db.rawQuery(sql,new String[]{i+""});
		}else {
			cursor = db.rawQuery(sql,null);
		}
		List<ListEntity> entities = new ArrayList<ListEntity>();
		ListEntity entity;
		while (cursor.moveToNext()) {
			entity = new ListEntity(context);
			entity.setTitle(cursor.getString(cursor.getColumnIndex("title")));
			entity.setParId(cursor.getInt(cursor.getColumnIndex("parId")));
			entity.setId(cursor.getInt(cursor.getColumnIndex("_id")));
			entity.setDesc(cursor.getString(cursor.getColumnIndex("description")));
			entity.setSrcUrl(cursor.getString(cursor.getColumnIndex("srcurl")));
			entity.setThumbnail(cursor.getString(cursor.getColumnIndex("thumbnail")));
			entity.setTime(cursor.getLong(cursor.getColumnIndex("insert_time")));
			entity.setUrls(cursor.getString(cursor.getColumnIndex("img_urls")));
			entity.setLoves(cursor.getInt(cursor.getColumnIndex("loves")));
			entities.add(entity);
		}
		return entities;
	}
	/**
	 * 查询某条信息后的10条信息
	 * @param resId 资源ID
	 * @param time 最小的插入时间
	 * @return
	 */
	public List<ListEntity> query(int resId) {
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = null;
		String sql = "";
		if (resId == 1) {
			sql = "select * from list order by insert_time desc limit 10";
			cursor = db.rawQuery(sql,new String[]{});
		}else {
			sql = "select * from list where parId = ? order by insert_time desc limit 10";
			cursor = db.rawQuery(sql,new String[]{resId+""});
		}
		
		List<ListEntity> entities = new ArrayList<ListEntity>();
		ListEntity entity;
		while (cursor.moveToNext()) {
			entity = new ListEntity(context);
			entity.setTitle(cursor.getString(cursor.getColumnIndex("title")));
			entity.setParId(cursor.getInt(cursor.getColumnIndex("parId")));
			entity.setId(cursor.getInt(cursor.getColumnIndex("_id")));
			entity.setDesc(cursor.getString(cursor.getColumnIndex("description")));
			entity.setSrcUrl(cursor.getString(cursor.getColumnIndex("srcurl")));
			entity.setThumbnail(cursor.getString(cursor.getColumnIndex("thumbnail")));
			entity.setTime(cursor.getLong(cursor.getColumnIndex("insert_time")));
			entity.setUrls(cursor.getString(cursor.getColumnIndex("img_urls")));
			entity.setLoves(cursor.getInt(cursor.getColumnIndex("loves")));
			entities.add(entity);
		}
		return entities;
	}
	public void insert(List<ListEntity> entities, String from) {
		for (ListEntity listEntity : entities) {
			insert(listEntity,from);
		}
	}

	public void updateLoves(int infoId) {
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		String sql = "update list set loves = loves + 1 where _id = ?";
		db.execSQL(sql,new Object[]{infoId});
	}
}
